import request from "../request";
import qs from "qs";

interface AdminLoginParams {
  password: string;
  username: string;
}

interface AdminLoginResData {
  token: string;
  tokenHead: string;
}

interface AdminInfoResData {
  menus: MenusType[];
}

interface AdminListParams {
  pageNum: number;
  keyword: string;
  pageSize: number;
}

interface AdminListResData {
  list: {}[];
  total: number;
}

interface RoleUpdateParams {
  adminId: number;
  roleIds: string;
}
interface RoleUpdateParams1 {
  adminId: number;
  roleIds: number[];
}

// type fn = () => void;

// 登录接口
export const adminLogin = (
  data: AdminLoginParams
): PromiseRes<AdminLoginResData> => request.post("/admin/login", data);

// 获取当前登录用户信息
export const getAdminInfo = (): PromiseRes<AdminInfoResData> =>
  request.get("/admin/info");

// 获取用户列表
export const getAdminList = (
  data: Partial<AdminListParams>
): PromiseRes<AdminListResData> => request.get("/admin/list", { params: data });

// 修改指定用户信息
export const updateAdmin = (data: AdminResDataObj): PromiseRes<unknown> =>
  request.post("/admin/update/" + data.id, data);

//用户注册、添加  /admin/register
export const registerAdmin = (data: AdminResDataObj): PromiseRes<unknown> =>
  request.post("/admin/register", data);

// 获取指定用户的角色列表
export const getAdminRoleById = (id: number): PromiseRes<{ id: number }[]> =>
  request.get("/admin/role/" + id);

// 给用户分配角色
export const updateAdminRole = (data: RoleUpdateParams): PromiseRes<unknown> =>
  request.post("/admin/role/update", null, { params: data });

export const updateAdminRole1 = (
  data: RoleUpdateParams1
): PromiseRes<unknown> => {
  console.log(qs.stringify(data, { arrayFormat: "repeat" }));
  return request.post(
    "/admin/role/update",
    qs.stringify(data, { arrayFormat: "repeat" })
  );
};
